﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.UI;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Administrator
{
    public partial class ProjectEdit : BasePage
    {
        private ProjectLogic projectLogic = null;
        private CatalogueLogic catalogueLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            InitCatalogueDDL();
            if (!IsPostBack)
            {
                InitProject();
            }
        }

        /// <summary>
        /// 载入栏目选择框
        /// </summary>
        private void InitCatalogueDDL()
        {
            this.SelectCatalogueDropDownList1.CatalogueLogicProvider = catalogueLogic;
            this.SelectCatalogueDropDownList1.CanClickNodeType = CatalogueTypeEnum.ProjectList;
        }

        /// <summary>
        /// 载入待编辑案例
        /// </summary>
        private void InitProject()
        {
            try
            {
                string action = Request.QueryString["action"];
                if (string.Equals(action, EditStateEnum.Edit.ToString(), StringComparison.CurrentCultureIgnoreCase))
                {
                    string strId = Request.QueryString["id"];
                    long id = 0;
                    if (long.TryParse(strId, out id))
                    {
                        btnSave.CommandName = EditStateEnum.Edit.ToString();
                        btnSave.CommandArgument = id.ToString();

                        Project project = projectLogic.GetById(id);
                        if (project != null)
                        {
                            ProjectNameTextBox.Text = project.ProjectName;
                            SelectCatalogueDropDownList1.SelectedCatalogueId = project.CatalogueId;
                            lblNotice.Visible = true;
                            ProjectDateTextBox.Text = project.ProjectDate.ToString();
                            txtProjectDiscription.Text = project.ProjectDiscription;
                        }
                        else
                        {
                            AddScript("alert('找不到该案例！');history.go(-1);");
                        }
                    }
                    else
                    {
                        AddScript("alert('参数传递错误！');history.go(-1);");
                    }
                }
                else
                {
                    btnSave.CommandName = EditStateEnum.New.ToString();
                }
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
            }
        }

        protected override void InitLogic()
        {
            projectLogic = new ProjectLogic();
            catalogueLogic = new CatalogueLogic();
        }

        protected override void DisposeLogic()
        {
            if (projectLogic != null)
            {
                projectLogic.Dispose();
                projectLogic = null;
            }
            if (catalogueLogic != null)
            {
                catalogueLogic.Dispose();
                catalogueLogic = null;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Project project = GetEditingProject();
                if (project != null)
                {
                    projectLogic.Save(project);
                    AddScript("alert('保存案例信息成功！');");
                }
                else
                {
                    AddScript("alert('要修改的案例信息已经不存在！');");
                }
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("alert('保存案例信息失败！');");
            }
        }

        private Project GetEditingProject()
        {
            Project project = null;

            if (btnSave.CommandName == EditStateEnum.Edit.ToString())
            {
                long id = long.Parse(btnSave.CommandArgument);
                project = projectLogic.GetById(id);
            }
            else
            {
                project = new Project();
                project.Id = projectLogic.GetNewIdentity();
                project.CreatedTime = DateTime.Now;
            }

            if (project != null)
            {
                project.CatalogueId = SelectCatalogueDropDownList1.SelectedCatalogueId;
                project.ProjectName = ProjectNameTextBox.Text;
                project.ProjectDiscription = txtProjectDiscription.Text;
                project.ProjectDate = DateTime.Parse(ProjectDateTextBox.Text);
                UploadImageAndGetIco(project);
            }
            return project;
        }

        /// <summary>
        /// 上传文件生成缩略图
        /// </summary>
        /// <param name="project"></param>
        private void UploadImageAndGetIco(Project project)
        {
            if (ProjectImageFileUpLoad.HasFile)
            {
                string[] paths = UtilityTool.GetNewPicPath(ProjectImageFileUpLoad.FileName);
                string physicalPath = paths[1];
                ProjectImageFileUpLoad.PostedFile.SaveAs(physicalPath);

                project.ProjectImage = paths[0];

                try
                {
                    //生成缩略图
                    string[] thumImagePaths = UtilityTool.GetIcoPicPath(paths[0]);
                    string thumbImagePhysicalPath = thumImagePaths[1];

                    projectLogic.GetThumbnailImage(physicalPath, thumbImagePhysicalPath);
                    project.ProjectIco = thumImagePaths[0];
                }
                catch
                {
                    project.ProjectIco = paths[0];
                }
            }
        }
    }
}